<?php
require_once 'Image/Graph.php';
require_once 'Image/Canvas.php';
//require_once '../maths/tool_maths.php';

class Tool_pear_graph{

  public $width=900;
  public $height=600;

  public $array_array_x=array();
  public $array_array_y=array();
  public $array_dot=array();

  public $max_x;
  public $max_y;

  public $min_x;
  public $min_y;

  public $interval_x;
  public $interval_y;

  public $plot_title=array();
  public $plot_color=array();

  public $title=array();

  public function __construct($_width=900,$_height=400){
    $this->width=$_width;
    $this->height=$_height;
  }

  public function __add_array_x($_array_x){
    $this->array_array_x[]=$_array_x;
  }
  public function __add_array_y($_array_y){
    $this->array_array_y[]=$_array_y;
  }


  /*
   * dot = (x,y,mark)
   */
  public function __add_array_dot($_x,$_y,$_mark){
    $this->array_dot[]=array($_x,$_y,$_mark);
  }

  public function __set_width($_width){
    $this->width=$_width;
  }
  public function __set_height($_height){
    $this->height=$_height;
  }

  public function __set_max_x($_max_x){
    $this->max_x=$_max_x;
  }
  public function __set_max_y($_max_y){
    $this->max_y=$_max_y;
  }
  public function __set_min_x($_min_x){
    $this->min_x=$_min_x;
  }

  public function __add_title($_title){
    $this->title=$_title;
  }

  public function __add_plot_title($_title,$_default=null){
    if($_default==null) $this->plot_title[]=$_title;
    else $this->plot_title[$_default]=$_title;
  }

  public function __add_plot_color($_color){
    $this->plot_color[]=$_color;
  }


  public function __get_basic_canvas(){
    $canvas = & Image_Canvas::factory('svg', array('width' => $this->width,
                                                   'height' => $this->height,
                                                   'encoding'=>'UTF-8'));
    return $canvas;
  } 

  public function __array_dot2dataset($_array_dot){
    $array_dataset=array();
    foreach($_array_dot as $id=>$dot){
      $array_dataset[$id] =& Image_Graph::factory('dataset');
      $array_dataset[$id]->addPoint($dot[0],$dot[1],$dot[2]);
    }
    return $array_dataset;
  }

  public function __array_corners2dataset($_array_corners){
    $array_dataset=array();
    foreach($_array_corners as $id=>$corners){
      $array_dataset[$id] =& Image_Graph::factory('dataset');
      foreach($corners as $corner){
        $array_dataset[$id]->addPoint($corner[0],$corner[1],$corner[2]);
      }
    }
    return $array_dataset;
  }

  public function __get_all_dataset(){
    $Dataset=array();
    foreach($this->array_array_x as $id1=>$array_x){
      $Dataset[$id1] =& Image_Graph::factory('dataset');
      foreach($array_x as $id2=>$x){
        if(isset($this->plot_title[$id1])){
          $Dataset[$id1]->addPoint($x, $this->array_array_y[$id1][$id2],$this->plot_title[$id1]); 
        }
        else{
          $Dataset[$id1]->addPoint($x, $this->array_array_y[$id1][$id2]);
        }
      }
    }
    return $Dataset;
  }

  public function __get_info_axis($Dataset){
    foreach($Dataset as $data){
      $amin_y[]=floor($data->minimumY());
      //$amin_y[]=$data->minimumY();
      $amax_y[]=ceil($data->maximumY());
      $amin_x[]=floor($data->minimumX());
      $amax_x[]=ceil($data->maximumX());
    }

    $min_y=min($amin_y); $min_x=min($amin_x);
    $max_y=max($amax_y); $max_x=max($amax_x);

    $step_y=tool_maths::get_ten(abs($max_y-$min_y))/10;
    $step_x=tool_maths::get_ten(abs($max_x-$min_x))/10;
/*
    $min_x=tool_maths::ceil_with_step($min_x,$step_x);
    $min_y=tool_maths::ceil_with_step($min_y,$step_y);
    $max_x=tool_maths::floor_with_step($max_x,$step_x);
    $max_y=tool_maths::floor_with_step($max_y,$step_y);
*/
    return array("x"=>array("min"=>$min_x,"max"=>$max_x,"step"=>$step_x),
                 "y"=>array("min"=>$min_y,"max"=>$max_y,"step"=>$step_y));
  }

  public function scatter_dot_group($_array_dot,$_array_group=null,$_corners=null){
    //create a canvas
    $Canvas=$this->__get_basic_canvas();
    $Graph =& Image_Graph::factory('graph', $Canvas);

    //create graph
    $Graph->add(
      Image_Graph::vertical(
        Image_Graph::factory('title', array($this->title,12)),
        Image_Graph::vertical(
          $Plotarea = Image_Graph::factory('plotarea',array('axis','axis')),
          $Legend = Image_Graph::factory('legend'),
          90
        ),
        5
      )
    );
    $Legend->setPlotarea($Plotarea);        
    $dataset_dot=$this->__array_dot2dataset($_array_dot);
    if($_array_group!=null)
      $dataset_group=$this->__array_dot2dataset($_array_group);

    $info=$this->__get_info_axis(array_merge($dataset_dot,$dataset_group));
//    $info=$this->__get_info_axis($dataset_dot);
    $Axis =& $Plotarea->getAxis(IMAGE_GRAPH_AXIS_X);
    $Axis->forceMaximum($info["x"]["max"]);  
    $Axis->forceMinimum($info["x"]["min"]);  
    if($info["x"]["step"]>1) $Axis->setLabelInterval($info["x"]["step"]/4);  

    $Axis =& $Plotarea->getAxis(IMAGE_GRAPH_AXIS_Y);
    $Axis->setAxisIntersection(0);
    $Axis->forceMaximum($info["y"]["max"]);  
    $Axis->forceMinimum($info["y"]["min"]);

    $dataset_corners=$this->__array_corners2dataset($_corners);

    foreach($dataset_corners as $i=>$data_corner){
      $area =& $Plotarea->addNew('Image_Graph_Plot_Area',array($data_corner));
      $area->setFillColor(tool_color::$array_color[$i].'@0.5');
      $area->setLineColor(tool_color::$array_color[$i].'@0.1');
      $first=$dataset_group[$i]->first();
      $area->setTitle($first["ID"]);
    }

    foreach($dataset_dot as $i=>$data_dot){
      $first=$data_dot->first();
      $Plot[$i] =& $Plotarea->addNew('Image_Graph_Plot_Dot', array($data_dot));
      $Marker[$i] =& Image_Graph::factory('Image_Graph_Marker_Value',IMAGE_GRAPH_POINT_ID);
      $Marker[$i]->setFillColor(tool_color::$array_color[$i]);
      $Marker[$i]->setLineColor(tool_color::$array_color[$i]);
      $Marker[$i]->setSize(3);
      $Marker[$i]->setPadding(1);
      $Plot[$i]->setMarker($Marker[$i]);
      $Plot[$i]->setTitle($first["ID"]);
    }

    foreach($dataset_group as $i=>$data_dot){
      $first=$data_dot->first();
      $Plot[$i] =& $Plotarea->addNew('Image_Graph_Plot_Dot', array($data_dot));
      $Marker[$i] =& Image_Graph::factory('Image_Graph_Marker_Circle');
      $Marker[$i]->setFillColor(tool_color::$array_color[$i]."@0.75");
      $Marker[$i]->setLineColor("#444444");
      $Marker[$i]->setSize(6);
      $Plot[$i]->setMarker($Marker[$i]);
      $Plot[$i]->setTitle($first["ID"]);
    }


    return $Graph;

  }


}
?>
